using IGeekFan.AspNetCore.Knife4jUI;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using RBAC_Application;
using RBAC_Application.Repository.Implement;
using RBAC_Application.Repository.Interface;
using RBAC_Application.Service.RBAC;
using RBAC_Model;
using SqlSugar;
using System.Text;
using Yitter.IdGenerator;
using ServiceManagement.Consul;
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(o =>
{
    var files = AppContext.BaseDirectory + "B.MES.Login.xml";
    o.IncludeXmlComments(files, true);

 

});

#region 数据库配置
// 注册SqlSugar
builder.Services.AddScoped<ISqlSugarClient>(s =>
{
    SqlSugarClient sqlSugar = new SqlSugarClient(new ConnectionConfig()
    {
        ConnectionString = builder.Configuration.GetConnectionString("conn"),
        DbType = DbType.MySql,
        IsAutoCloseConnection = true,
    },
    db =>
    {
        db.Aop.OnLogExecuting = (sql, pars) =>
        {
            Console.WriteLine(UtilMethods.GetNativeSql(sql, pars));
        };
    });
    return sqlSugar;
});





// 初始化数据库表
var db = new SqlSugarClient(new ConnectionConfig()
{
    ConnectionString = builder.Configuration.GetConnectionString("conn"),
    DbType = DbType.MySql,
    IsAutoCloseConnection = true,
});

db.CodeFirst.InitTables<User>();
db.CodeFirst.InitTables<Role>();
db.CodeFirst.InitTables<Permission>();
db.CodeFirst.InitTables<UserRole>();
db.CodeFirst.InitTables<RolePermission>();
#endregion

//注册consul
builder.Configuration.Register();








#region RBAC服务注册
builder.Services.AddScoped<IUserRepository, UserRepository>();
builder.Services.AddScoped<IRoleRepository, RoleRepository>();
builder.Services.AddScoped<IPermissionRepository, PermissionRepository>();
builder.Services.AddScoped<IRbacService, RbacService>();
YitIdHelper.SetIdGenerator(new IdGeneratorOptions());



#endregion
  



 
var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseKnife4UI();
 //   app.UseSwaggerUI();
}
app.UseAuthentication(); // 认证
 
app.UseCors(x => x.AllowAnyHeader().AllowAnyOrigin().AllowAnyMethod());
app.UseAuthorization();
app.UseStaticFiles();
app.MapControllers();

app.Run();
